package com.my.transaction.dao.impl;

import com.my.transaction.entity.Account;
import com.my.transaction.JDBCUtil;
import com.my.transaction.dao.AccountDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;

public class AccountDaoImpl implements AccountDao {

    @Override
    public Account queryAccountByCard(int cardId) throws SQLException {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtil.getConnection();
        String sql = "select * from account where cardid = ?";
        Account account = runner.query(conn,sql,new BeanHandler<Account>(Account.class),cardId);
        return account;
    }

    @Override
    public void updateAccount(Account account) throws SQLException {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtil.getConnection();
        String sql = "update account set balance = ? where cardid = ?";
        runner.update(conn,sql,new Object[]{account.getBalance(),account.getCardid()});
    }
}
